package com.skt.common_hr.mintview.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.mintview.MintViewFacade;
	import com.skt.common_hr.mintview.events.NoticeNewEvent;
	import com.skt.common_hr.mintview.model.PrivateListProxy;
	import com.skt.common_hr.mintview.model.ProfileDetailProxy;
	import com.skt.common_hr.mintview.model.ProfileProxy;
	import com.skt.common_hr.mintview.view.components.PrivateOverView;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	public class ProfileMediator extends CommonMediator
	{
		public static const NAME:String = "ProfileMediator"; 

		public static const CLOSED_REQUEST_VIEW:String	= "closedRequestView";
		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function ProfileMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void
 		{
 			setEventListener();		
 			view.setDateRange();
 		}
 		
 		private function setEventListener():void
 		{
			view.addEventListener(PrivateOverView.PrivateListSearch, loadSearchData);
			view.addEventListener(PrivateOverView.PrivateWriteClose, goMainList);
			view.addEventListener(NoticeNewEvent.SELECTED_ITEM_NEW, onDetailView);
			view.addEventListener('CheckPrCard', onPrCardCheck);
//			view.addEventListener(PrivateOverView.PrivateListData, loadListData);
 		}
 		
 		private function removeEventListeners():void{
 			view.removeEventListener(PrivateOverView.PrivateListSearch, loadSearchData);
			view.removeEventListener(PrivateOverView.PrivateWriteClose, goMainList);
			view.removeEventListener(NoticeNewEvent.SELECTED_ITEM_NEW, onDetailView);
			view.removeEventListener('CheckPrCard', onPrCardCheck);
 		}
 		
 		private function loadSearchData(event:Event = null):void
 		{
 			var privateListProxy:PrivateListProxy = facade.retrieveProxy(PrivateListProxy.NAME) as PrivateListProxy;
 			privateListProxy.load(view.dataProvider.PERNR , Variables.myPernr, view.sDay, view.eDay);	
 		}
 		
 		public var _pernr:String;
		private function loadProfile(pernr:String):void
		{
 			_pernr = pernr;
 			var basicProxy:ProfileProxy = facade.retrieveProxy(ProfileProxy.NAME) as ProfileProxy;
 			basicProxy.load(pernr);
		}
		
		private function loadProfileDetail(pernr:String):void
		{
			var detailProxy:ProfileDetailProxy = facade.retrieveProxy(ProfileDetailProxy.NAME) as ProfileDetailProxy;
			detailProxy.load(pernr);
		}
		
		private function loadProfilePrList(pernr:String):void
		{
			var privateListProxy:PrivateListProxy = facade.retrieveProxy(PrivateListProxy.NAME) as PrivateListProxy;
 			privateListProxy.load(pernr , Variables.myPernr, view.sDay, view.eDay);
		}
		
		private function prBtnEnabledChkFun(pernr:String):void{
 			var privateListProxy:PrivateListProxy = facade.retrieveProxy(PrivateListProxy.NAME) as PrivateListProxy;
 			privateListProxy.loadBtnChk(pernr , Variables.myPernr);	
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					MintViewFacade.CHANGE_SELECTED_MEMBER,
					MintViewFacade.LOAD_PROFILE_COMPLETE,
					MintViewFacade.LOAD_PROFILE_FAILED,
					MintViewFacade.LOAD_PRIVATELIST_COMPLETE,
					MintViewFacade.LOAD_PRIVATELIST_FAILED,
					MintViewFacade.LOAD_BTNCHECK_COMPLETE,
					MintViewFacade.LOAD_BTNCHECK_FAILED,
					MintViewFacade.LOAD_PROFILE_PRCARD_CHECK_COMPLETE,
					MintViewFacade.LOAD_PROFILE_PRCARD_CHECK_FAILED,
					MintViewFacade.LOAD_PROFILE_DETAIL_COMPLETE,
					MintViewFacade.LOAD_PROFILE_DETAIL_FAILED 
					];
		}
		
		
		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case MintViewFacade.CHANGE_SELECTED_MEMBER:
					loadProfile(String(note.getBody()));
					break;
				case MintViewFacade.LOAD_PROFILE_COMPLETE:
					onSucceedDataLoad(note.getBody() as ProfileProxy);
					break;
				case MintViewFacade.LOAD_PROFILE_FAILED:
					break;
				case MintViewFacade.LOAD_PRIVATELIST_COMPLETE:
					onSucceedprivateViewLoad(note.getBody() as PrivateListProxy);
					
					break;
				case MintViewFacade.LOAD_PRIVATELIST_FAILED:
					break;	
				case MintViewFacade.LOAD_BTNCHECK_COMPLETE:
					view.btnPrc.visible = true;
					view.btnPrc.includeInLayout = true;
					break;
				case MintViewFacade.LOAD_BTNCHECK_FAILED:
					view.btnPrc.visible = false;
					view.btnPrc.includeInLayout = false;
					break;	
				case MintViewFacade.LOAD_PROFILE_PRCARD_CHECK_COMPLETE:
					onSucceedPrCardCheck(note.getBody() as ProfileDetailProxy);
					break;
				case MintViewFacade.LOAD_PROFILE_PRCARD_CHECK_FAILED:
					trace("LOAD_PROFILE_PRCARD_CHECK_FAILED");
					onFailedPrCardCheck(note.getBody() as ProfileDetailProxy);
					break;		
				//개인신상
				case MintViewFacade.LOAD_PROFILE_DETAIL_COMPLETE:
					onSucceedDetailDataLoad(note.getBody() as ProfileDetailProxy);
					break;
				case MintViewFacade.LOAD_PROFILE_DETAIL_FAILED:
					trace("LOAD_PROFILEDETAIL_FAILED");
					break;
				// 전표출력..
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		/**
		 * 인사기록카드 출력 체크여부, silver
		 * @param
		 * @return void
		 */
 		private function onPrCardCheck(event:Event):void
 		{
 			var prCardCheckProxy:ProfileDetailProxy = facade.retrieveProxy(ProfileDetailProxy.NAME) as ProfileDetailProxy;
			prCardCheckProxy.loadPrCardCheck(Variables.myPernr);
 		}

		private function onDetailView(event:NoticeNewEvent):void
		{
			facade.sendNotification(MintViewFacade.LOAD_LIST_NEW_VIEW, this);
		}
		
		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function onSucceedDataLoad(proxy:ProfileProxy):void
 		{
 			trace("onSucceedDataLoad");
 			view.dataProvider = proxy.profile;
			loadProfileDetail(_pernr);
			prBtnEnabledChkFun(_pernr);			 			
 		}
 		
 		private function onSucceedprivateViewLoad(proxy:PrivateListProxy):void
 		{
 			trace("onSucceedDataLoad");
 			view.privateViewList = proxy.mintListArr;
 		}
 		
 		private function onSucceedPrCardCheck(proxy:ProfileDetailProxy):void
 		{
 			var check:Boolean = proxy.prCheck;
 			view.clickOutputBtn(check);
 		}
 		
 		private function onFailedPrCardCheck(proxy:ProfileDetailProxy):void
 		{
 			var check:Boolean = proxy.prCheck;
 			view.clickOutputBtn(check);
 		}
 		
 		private function onSucceedDetailDataLoad(proxy:ProfileDetailProxy):void
 		{
 			loadProfilePrList(_pernr);
 			view.scholarshipArr = proxy.scholarship;
 			view.familyArr = proxy.family;
 			view.skCareerArr = proxy.skCareer2;
 			view.languageArr = proxy.language;
 			view.licenseArr = proxy.license;
 			view.awardDisciplinaryArr = proxy.awardDisciplinary;
 			view.officalAnnArr = proxy.officalAnn;
 			
 			view.acAppraisalResult = proxy.acAppraisalResult;  
 		}
 		
 		private function goMainList(event:Event):void
 		{
 			clearView();
 			bActivated = false;
 			facade.sendNotification(CLOSED_REQUEST_VIEW, this);
 		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():PrivateOverView
        {
            return viewComponent as PrivateOverView;
        }
	}
}